package cn.rui.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.rui.common.ResponseBean;
import cn.rui.pojo.exam.ExamQuestion;
import cn.rui.pojo.vo.PageVo;
import cn.rui.service.exam.QuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
@RequestMapping("question")
@Api(tags = "试题模块")
@Slf4j
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @PostMapping("findPage")
    @SaCheckLogin
    @ApiOperation(value = "分页查询试题列表")
    public ResponseBean findPage(@RequestBody PageVo pageVo, HttpSession session) {
        return questionService.findPage (pageVo, session);
    }

    @DeleteMapping("deleteById/{id}")
    @SaCheckLogin
    @ApiOperation(value = "根据id删除试题")
    public ResponseBean deleteById(@PathVariable("id") Long id) {
        return questionService.deleteById (id);
    }

    @PostMapping("save")
    @SaCheckLogin
    @ApiOperation(value = "添加试题")
    public ResponseBean save(@RequestBody ExamQuestion examQuestion, HttpSession session) {
        return questionService.save (examQuestion, session);
    }

    @GetMapping(value = "downloadTemplate")
    @ApiOperation(value = "下载试题导入模板")
    public void downloadTemplate(HttpServletRequest request, HttpServletResponse response) throws IOException {
        questionService.downloadTemplate (request, response);
    }

    @PostMapping("imports/{username}")
    @ApiOperation(value = "批量导入试题")
    public ResponseBean imports(MultipartFile file, @PathVariable("username") String username) throws IOException {
        return questionService.imports (file, username);
    }
}